package com.netease.demo.live.actvity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.demo.live.R;
import com.netease.demo.live.base.TActivity;
import com.netease.demo.live.permission.MPermission;
import com.netease.demo.live.permission.annotation.OnMPermissionDenied;
import com.netease.demo.live.permission.annotation.OnMPermissionGranted;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hzxuwen on 2016/5/19.
 */
public class EnterActivity extends TActivity {
    @Bind(R.id.live_url_edit)
    EditText liveUrlEdit;
    @Bind(R.id.enter_live_btn)
    TextView enterLiveBtn;

    private final static String EXTRA_MODE = "EXTRA_MODE";
    private final int BASIC_PERMISSION_REQUEST_CODE = 110;
    private static boolean isEnable = false;

    private boolean isLive = true; // 拉流 true， 推流 false

    public static void startActivity(Context context, boolean isLive) {
        Intent intent = new Intent();
        intent.setClass(context, EnterActivity.class);
        intent.putExtra(EXTRA_MODE, isLive);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.app_name);
        setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(R.drawable.ic_back);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ButterKnife.bind(this);

        isLive = getIntent().getBooleanExtra(EXTRA_MODE, true);
        if (isLive) {
//            liveUrlEdit.setText("rtmp://p1.live.126.net/live/b9820aa0c25b410788891db166901e79?wsSecret=c5ebe45af100e1223fb5b1a4ddf2b798&wsTime=1463832272");
            liveUrlEdit.addTextChangedListener(textWatcher);
            enterLiveBtn.setText(R.string.enter_live);
            toolbar.setTitle(R.string.master);
        } else {
//            liveUrlEdit.setText("rtmp://v1.live.126.net/live/b9820aa0c25b410788891db166901e79");
            liveUrlEdit.addTextChangedListener(textWatcher);
            enterLiveBtn.setText(R.string.play);
            toolbar.setTitle(R.string.audience);
        }
        updateEnterLiveBtn(false);
    }

    @OnClick(R.id.enter_live_btn)
    public void onClick() {
        if (isLive) {
            LiveActivity.startActivity(EnterActivity.this, liveUrlEdit.getText().toString(), true);
        } else {
            requestBasicPermission(); // 申请APP基本权限
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            updateEnterLiveBtn(liveUrlEdit.getText().length() != 0);
        }
    };

    private void updateEnterLiveBtn(boolean isEnable) {
        enterLiveBtn.setEnabled(isEnable);
        enterLiveBtn.setBackgroundResource(R.drawable.enter_button_selector);
        if (isEnable) {
            enterLiveBtn.setTextColor(getResources().getColor(R.color.white));
        } else {
            enterLiveBtn.setTextColor(getResources().getColor(R.color.color_black_20));
        }
    }


    /**
     * 基本权限管理
     */
    private void requestBasicPermission() {
        MPermission.with(EnterActivity.this)
                .addRequestCode(BASIC_PERMISSION_REQUEST_CODE)
                .permissions(
                        Manifest.permission.READ_PHONE_STATE)
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @OnMPermissionGranted(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionSuccess(){
        AudienceActivity.startActivity(EnterActivity.this, liveUrlEdit.getText().toString());
//        Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
    }

    @OnMPermissionDenied(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionFailed(){
        Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
    }
}
